Node.js-এর জন্য উন্নত টাইপস্ক্রিপ্ট
এই গাইডটি উন্নত TypeScript বৈশিষ্ট্য এবং নিদর্শনগুলি অন্বেষণ করে যা বিশেষ করে Node.js অ্যাপ্লিকেশনগুলির জন্য দরকারী৷
বিস্তারিত টাইপস্ক্রিপ্ট ডকুমেন্টেশনের জন্য:
বিস্তারিত TypeScript ডকুমেন্টেশনের জন্য আমাদের TypeScript টিউটোরিয়াল দেখুন।
উন্নত ধরনের সেটিং বৈশিষ্ট্য
TypeScript এর টাইপ সিস্টেম শক্তিশালী এবং রক্ষণাবেক্ষণযোগ্য Node.js অ্যাপ্লিকেশন তৈরির জন্য শক্তিশালী সরঞ্জাম সরবরাহ করে।
এখানে মূল বৈশিষ্ট্য আছে:
1. ইউনিয়ন এবং ছেদ এর প্রকার
// Union type
function formatId(id: string | number) {
return `ID: ${id}`;
}
// Intersection type
type User = { name: string } & { id: number };
2. ক্যাটাগরি কার্ড
type Fish = { swim: () => void };
type Bird = { fly: () => void };
function isFish(pet: Fish | Bird): pet is Fish {
return 'swim' in pet;
}
3. উন্নত জেনেরিক
// Generic function with constraints
function getProperty(obj: T, key: K): T[K] {
return obj[key];
}
// Generic interface with default type
interface PaginatedResponse {
data: T[];
total: number;
page: number;
limit: number;
}
// Using generic types with async/await in Node.js
async function fetchData(url: string): Promise {
const response = await fetch(url);
return response.json();
}
4. ম্যাপ করা এবং শর্তাধীন প্রকার
// Mapped types
type ReadonlyUser = {
readonly [K in keyof User]: User[K];
};
// Conditional types
type NonNullableUser = NonNullable; // User
// Type inference with conditional types
type GetReturnType = T extends (...args: any[]) => infer R ? R : never;
function getUser() {
return { id: 1, name: 'Alice' } as const;
}
type UserReturnType = GetReturnType; // { readonly id: 1; readonly name: "Alice"; }
5. বিভাগ ভবিষ্যদ্বাণী এবং বিভাগ কার্ড
TypeScript এর টাইপ ইনফারেন্স এবং টাইপ কার্ড কম পয়েন্টার সহ টাইপ-সেফ কোড তৈরি করতে সাহায্য করে:
// Type inference with variables
const name = 'Alice'; // TypeScript infers type: string
const age = 30; // TypeScript infers type: number
const active = true; // TypeScript infers type: boolean
// Type inference with arrays
const numbers = [1, 2, 3]; // TypeScript infers type: number[]
const mixed = [1, 'two', true]; // TypeScript infers type: (string | number | boolean)[]
// Type inference with functions
function getUser() {
return { id: 1, name: 'Alice' }; // Return type inferred as { id: number; name: string; }
}
const user = getUser(); // user inferred as { id: number; name: string; }
console.log(user.name); // Type checking works on inferred properties
Node.js-এর জন্য উন্নত টাইপস্ক্রিপ্ট প্যাটার্ন
এই নিদর্শনগুলি আরও রক্ষণাবেক্ষণযোগ্য এবং টাইপ-নিরাপদ Node.js অ্যাপ্লিকেশন তৈরি করতে সহায়তা করে:
1. উন্নত ডেকোরেটর
// Parameter decorator with metadata
function validateParam(target: any, key: string, index: number) {
const params = Reflect.getMetadata('design:paramtypes', target, key) || [];
console.log(`Validating parameter ${index} of ${key} with type ${params[index]?.name}`);
}
// Method decorator with factory
function logExecutionTime(msThreshold = 0) {
return function (target: any, key: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
const start = Date.now();
const result = await originalMethod.apply(this, args);
const duration = Date.now() - start;
if (duration > msThreshold) {
console.warn(`[Performance] ${key} took ${duration}ms`);
}
return result;
};
};
}
class ExampleService {
@logExecutionTime(100)
async fetchData(@validateParam url: string) {
// Implementation
}
}
2. উন্নত অ্যাপ্লিকেশন প্রকার
// Built-in utility types with examples interface User {
id: number;
name: string;
email?: string;
createdAt: Date;
}
// Create a type with specific properties as required
type AtLeast = Partial & Pick;
type UserCreateInput = AtLeast; // Only name is required
// Create a type that makes specific properties required
WithRequired = T & { [P in K]-?: T[P] };
type UserWithEmail = WithRequired;
// Extract function return type as a type
type UserFromAPI = Awaited>;
3. টাইপ-নিরাপদ ইভেন্ট এমিটার
import { EventEmitter } from 'events';
type EventMap = {
login: (userId: string) => void;
logout: (userId: string, reason: string) => void;
error: (error: Error) => void;
};
class TypedEventEmitter void>> {
private emitter = new EventEmitter();
on(event: K, listener: T[K]): void {
this.emitter.on(event as string, listener as any);
}
emit(
event: K,
...args: Parameters
): boolean {
return this.emitter.emit(event as string, ...args);
}
}
// Usage
const userEvents = new TypedEventEmitter();
userEvents.on('login', (userId) => {
console.log(`User ${userId} logged in`);
});
// TypeScript will show an error for incorrect argument types
// userEvents.emit('login', 123);
// Error: Argument of type 'number' is not assignable to 'string'
Node.js-এর জন্য TypeScript সেরা অনুশীলন
মূল ধারণা:
উন্নত ধরনের সেটিং ব্যবহার করুন
উন্নত কোড নিরাপত্তা এবং বিকাশকারী অভিজ্ঞতার জন্য TypeScript এর উন্নত টাইপসেটিং ব্যবহার করুন
জেনেরিক ব্যবহার করুন
প্রকার নিরাপত্তা না হারিয়ে নমনীয় এবং পুনরায় ব্যবহারযোগ্য উপাদান তৈরি করতে জেনেরিক ব্যবহার করুন
ডেকোরেটর সক্রিয় করুন
লগিং, বৈধতা এবং কর্মক্ষমতা পর্যবেক্ষণের মতো ক্রস-কাটিং উদ্বেগের জন্য ডেকোরেটর প্রয়োগ করুন
অ্যাপ্লিকেশন প্রকার ব্যবহার করুন
কোড ডুপ্লিকেশন ছাড়াই প্রকারগুলি সংশোধন এবং ম্যানিপুলেট করতে অ্যাপ্লিকেশন প্রকারগুলি ব্যবহার করুন৷
টাইপ-নিরাপদ বৈশিষ্ট্য তৈরি করুন
Node.js-নির্দিষ্ট প্যাটার্নের জন্য টাইপ-নিরাপদ বৈশিষ্ট্য তৈরি করুন যেমন ইভেন্ট ইমিটার এবং স্ট্রিম
কর্মক্ষমতা বিবেচনা:
বিস্তারিত টাইপস্ক্রিপ্ট ডকুমেন্টেশনের জন্য:
বিস্তারিত TypeScript ডকুমেন্টেশন এবং উদাহরণের জন্য আমাদের TypeScript টিউটোরিয়াল দেখুন।